package com.beimihu.leetcode;

public class TwoElementSum {
    // https://leetcode.cn/problems/two-sum-ii-input-array-is-sorted/?envType=study-plan-v2&envId=top-interview-150

    //超时了
    public int[] twoSum(int[] numbers, int target) {
        int p = 0;
        int q = 1;

        for(int i = 0 ; i < numbers.length - 1; i++){
            for(int j = i + 1 ; j <= numbers.length - 1; j++ ){
                if( numbers[i] + numbers [j] == target){
                    return new int[]{i + 1, j + 1};
                }
            }
        }
        return new int[]{-1, -1};
    }


    public int[] twoSumV2(int[] numbers, int target) {
        int low = 0 , high = numbers.length - 1;
        while(low < high){
            int sum  = numbers[low] + numbers[high];
            if(sum == target){
                return new int[] {low + 1, high + 1};
            }
            //如果低位加高位小于这个目标值，就让低位变大
            else if( sum < target ){
                low++;
            }
            else{
                high--;
            }
        }
        return new int[]{-1, -1};
    }


}
